home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / Frontier 4.0.1 / Frontier SDK 4.0b2 / Sample Code / Apple Events 101 / Client / client.c next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  2.5 KB  |  158 lines  |  [TEXT/CWIE]

  1.  
  2. /*© copyright 1991-96 UserLand Software, Inc. All Rights Reserved.*/
  3.  
  4.  
  5. #include <AppleEvents.h>
  6.  
  7.  
  8. static void initMacintosh (void) {
  9.  
  10.     /*
  11.     the magic stuff that every Macintosh application needs to do 
  12.     before doing anything else.
  13.     */
  14.  
  15.     register short i;
  16.         
  17.     MaxApplZone ();
  18.     
  19.     for (i = 0; i < 10; i++) 
  20.         MoreMasters ();
  21.     
  22.     InitGraf (&qd.thePort);
  23.     
  24.     InitFonts ();
  25.     
  26.     FlushEvents (everyEvent, 0);
  27.     
  28.     InitWindows ();
  29.     
  30.     InitMenus ();
  31.     
  32.     TEInit ();
  33.     
  34.     InitDialogs (0L);
  35.     
  36.     InitCursor ();
  37.     
  38.     for (i = 0; i < 5; i++) { /*register with Multifinder*/
  39.         
  40.         EventRecord ev;
  41.         
  42.         EventAvail (everyEvent, &ev); /*see TN180 -- splash screen*/
  43.         } /*for*/
  44.     } /*initMacintosh*/
  45.  
  46.  
  47. static Boolean sendMessageToServer (OSType appid, OSType eventclass, OSType eventid, short i) {
  48.     
  49.     AEAddressDesc adr = {typeNull, nil}; 
  50.     AppleEvent event = {typeNull, nil};
  51.     AppleEvent reply = {typeNull, nil};
  52.     OSErr ec;
  53.     
  54.     /*1. create the Apple Event, address it to the server app*/ {
  55.     
  56.         ec = AECreateDesc (typeApplSignature, (Ptr) &appid, sizeof (appid), &adr);
  57.         
  58.         if (ec != noErr)
  59.             goto error;
  60.             
  61.         ec = AECreateAppleEvent (
  62.             eventclass, eventid, &adr, 
  63.             
  64.             kAutoGenerateReturnID, kAnyTransactionID, 
  65.             
  66.             &event);
  67.             
  68.         if (ec != noErr)
  69.             goto error;
  70.         }
  71.     
  72.     /*2. convert the number to a string, push it onto the Apple Event*/ {
  73.         
  74.         Str255 s;
  75.         
  76.         NumToString (i, s);
  77.         
  78.         if (AEPutParamPtr (&event, keyDirectObject, typeChar, (Ptr) &s [1], s [0]) != noErr)
  79.             goto error;
  80.         }
  81.         
  82.     /*3. send the message, wait for a reply*/ {
  83.     
  84.         ec = AESend (        
  85.             &event, &reply, kAEWaitReply + kAECanInteract + kAECanSwitchLayer, 
  86.             
  87.             kAENormalPriority, kNoTimeOut, nil, nil);
  88.             
  89.         if (ec != noErr)
  90.             goto error;
  91.         }
  92.     
  93.     /*4. make sure we got the correct reply*/ {
  94.     
  95.         DescType actualtype;
  96.         Size actualsize;
  97.         long num;
  98.  
  99.         ec = AEGetParamPtr (
  100.             &reply, keyDirectObject, typeLongInteger, 
  101.         
  102.             &actualtype, (Ptr) &num, sizeof (num), &actualsize);
  103.             
  104.         if (ec != noErr)
  105.             goto error;
  106.         
  107.         if (num != i) {
  108.             
  109.             DebugStr ("\pThe reply is not what we expected.");
  110.             
  111.             goto error;
  112.             }
  113.         }
  114.     
  115.     AEDisposeDesc (&adr);
  116.         
  117.     AEDisposeDesc (&event);    
  118.     
  119.     AEDisposeDesc (&reply);    
  120.     
  121.     return (true);
  122.     
  123.     error:
  124.     
  125.     /*report the error code using DebugStr*/ {
  126.         
  127.         Str255 s;
  128.         
  129.         NumToString (ec, s);
  130.         
  131.         DebugStr (s);
  132.         }
  133.     
  134.     AEDisposeDesc (&adr);
  135.         
  136.     AEDisposeDesc (&event);    
  137.     
  138.     AEDisposeDesc (&reply);    
  139.     
  140.     return (false);
  141.     } /*sendMessageToServer*/
  142.     
  143.     
  144. void main (void) {
  145.     
  146.     short i;
  147.     
  148.     initMacintosh ();
  149.     
  150.     for (i = 1; i <= 100; i++) 
  151.         if (!sendMessageToServer ('AESV', 'SERV', 'DISP', i))
  152.             break;
  153.     } /*main*/
  154.  
  155.  
  156.  
  157.  
  158.